<template>
    <!-- 送审 -->
    <el-dialog :model-value="qualificationsDialogVisible" title="选择送审品类" width="520"
        :before-close="qualificationsBeforeClose">
        <el-form ref="qualificationsRef" :model="qualificationsData" style="width: 100%" :inline="true"
            :rules="qualificationsrules" label-width="80px" @submit.prevent>
            <el-row class="hm-row3" :gutter="60">
                <el-col :span="24">
                    <el-form-item label="可选品类" prop="productType">
                        <el-select style="width: 320px" v-model="qualificationsData.productType" placeholder="请选择可选品类"
                            filterable clearable>
                            <el-option v-for="item in categoryColumns" :key="item.value" :label="item.text"
                                :value="item.value"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="qualificationsBeforeClose">取消</el-button>
                <el-button :loading="buttonLoading" type="primary" @click="qualificationHandleSave(qualificationsRef)">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script setup name="QualificationsDialog" lang="ts">
import { ElMessage, FormInstance } from 'element-plus'
import { type Action } from 'element-plus'
const props = defineProps(['qualificationsDialogVisible', 'id'])
const emit = defineEmits(['close', 'submit'])
import { editSevens, getSystemList } from '@/api/group/product/index'
const qualificationsRef = ref<FormInstance>()
const qualificationsData = ref({
    productType: ''
})
const buttonLoading = ref(false);
// 资质审核可选品类column
const categoryColumns = ref<any>([])
const qualificationsrules = ref({
    productType: [
        { required: true, message: '请选择可选品类', trigger: 'change' },
    ],
})
const categoryMap: any = {
    '鞋帽': '1',
    '酒茶': '2',
    '箱包': '3',
    '百货': '4',
    '服装': '5',
    '食品': '6'
}
/** 可选品类column */
const getcategoryColumns = async () => {
    const res = await getSystemList({ parentId: '1796913597400784898' })
    if (res.data.length) {
        // 去掉惠麦优选部门
        const response = res.data.filter((item: any) => item.deptName !== '惠麦优选')
        response.forEach((item: any) => {
            item.text = item.deptName
            Object.keys(categoryMap).forEach(key => {
                if (item.deptName.includes(key)) {
                    item.value = categoryMap[key];
                    return;
                }
            });
        })
        categoryColumns.value = response
    }
}
getcategoryColumns()
/** 关闭 */
const qualificationsBeforeClose = () => {
    qualificationsRef.value?.resetFields()
    emit('close')
}
/** 提交 */
const qualificationHandleSave = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
        if (valid) {
            ElMessageBox.confirm('是否确认发起核价初审审批？', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                callback: async (action: Action) => {
                    if (action == 'confirm') {
                        buttonLoading.value = true;
                        try{
                            await editSevens({ ...qualificationsData.value, ids: props.id })
                            ElMessage.success('商品送审成功')
                            qualificationsRef.value?.resetFields()
                            buttonLoading.value = false
                            emit('submit')
                        }catch{
                            buttonLoading.value = false
                        }
                    }
                },
            })
        }
    })
}
</script>